//
//  VideoPlayerView.swift
//  UGSwiftMagicbox
//
//  Created by MBA on 2020/10/3.
//  Copyright © 2020 MBA. All rights reserved.
//

import SwiftUI
import AVFoundation
struct VideoPlayerView: UIViewRepresentable {
    
    
    @Binding private(set) var videoPos: Double
    @Binding private(set) var videoDuration: Double
    @Binding private(set) var seeking: Bool
    @Binding private(set) var status: AVPlayer.TimeControlStatus
    
    let player: AVPlayer
    
    func updateUIView(_ uiView: UIView, context: UIViewRepresentableContext<VideoPlayerView>) {
   
    }
    
    func makeUIView(context: UIViewRepresentableContext<VideoPlayerView>) -> UIView {
        let uiView = VideoPlayerUIView(player: player,
                                       videoPos: $videoPos,
                                       videoDuration: $videoDuration,
                                       seeking: $seeking,
                                       status: $status)
        return uiView
    }
    
    static func dismantleUIView(_ uiView: UIView, coordinator: ()) {
        guard let playerUIView = uiView as? VideoPlayerUIView else {
            return
        }
        playerUIView.cleanUp()
    }
}



struct VideoPlayerView_Previews: PreviewProvider {
    static var previews: some View {
        VideoPlayerView(videoPos: .constant(0) ,
                        videoDuration: .constant(0),
                        seeking: .constant(false),
                        status: .constant(AVPlayer.TimeControlStatus.paused),
                        player: AVPlayer(url: URL(string: "https://bitdash-a.akamaihd.net/content/sintel/hls/playlist.m3u8")!))
    }
}
